package com.production.CoursePlatform.controller;

import com.production.CoursePlatform.entity.Teaching;
import com.production.CoursePlatform.service.TeachingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teaching")
public class TeachingController {

    @Autowired
    private TeachingService teachingService;

    // 添加教学信息
    @PostMapping("/add")
    public ResponseEntity<String> addTeaching(@RequestBody Teaching teaching) {
        int result = teachingService.addTeaching(teaching);
        return result > 0 ? ResponseEntity.ok("添加成功") : ResponseEntity.status(500).body("添加失败");
    }

    // 删除教学信息
    @DeleteMapping("/delete/{ctID}")
    public ResponseEntity<String> deleteTeaching(@PathVariable int ctID) {
        int result = teachingService.deleteTeaching(ctID);
        return result > 0 ? ResponseEntity.ok("删除成功") : ResponseEntity.status(500).body("删除失败");
    }

    // 更新教学信息
    @PutMapping("/update")
    public ResponseEntity<String> updateTeaching(@RequestBody Teaching teaching) {
        int result = teachingService.updateTeaching(teaching);
        return result > 0 ? ResponseEntity.ok("更新成功") : ResponseEntity.status(500).body("更新失败");
    }

    // 根据 tID 查找教学信息
    @GetMapping("/find/{tID}")
    public ResponseEntity<List<Teaching>> getTeachingByTID(@PathVariable String tID) {
        List<Teaching> teachings = teachingService.getTeachingByTID(tID);
        return teachings != null ? ResponseEntity.ok(teachings) : ResponseEntity.status(404).body(null);
    }

    // 获取所有教学信息
    @GetMapping("/all")
    public ResponseEntity<List<Teaching>> getAllTeachings() {
        List<Teaching> teachings = teachingService.getAllTeachings();
        return teachings.isEmpty() ? ResponseEntity.status(404).body(null) : ResponseEntity.ok(teachings);
    }
}

